import os
import logging
from time import strftime
from utils.project_path import config


class Log:

    def __init__(self):
        self.__now = strftime("%Y-%m-%d-%H")
        self.__log_name = os.path.join(config.log_path, '{0}.log'.format(self.__now))

        # 创建一个logger
        self.__logger = logging.getLogger()
        self.__logger.setLevel(logging.INFO)
        # 创建一个handler，用于写入日志文件
        fh = logging.FileHandler(self.__log_name, 'a', encoding='utf-8')
        fh.setLevel(logging.INFO)

        sh = logging.StreamHandler()
        sh.setLevel(logging.INFO)

        # 定义handler的输出格式
        formatter = logging.Formatter(fmt=self.fmt)
        fh.setFormatter(formatter)
        sh.setFormatter(formatter)
        # 给logger添加handler
        self.__logger.addHandler(fh)
        self.__logger.addHandler(sh)

    def get_logger(self):
        if self.__logger is not None:

            return self.__logger

    @property
    def fmt(self):
        return '%(asctime) s [%(filename) s:%(funcName)s:%(lineno)-2s] [%(levelname) 2s] %(message) s'


log = Log().get_logger()